<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Request;

class Blog extends Controller
{
    /**
     * 显示资源列表
     * index主页面
     * blogadd 添加博文
     * blogupd 修改博文
     * blogdel 删除博文
     * blogsearch 搜索博文
     * @return \think\Response
     */
    public function index()
    {
        $data = (new Admin())->insert();
        $dete = (new \app\admin\model\Blog())->blogselect();
        $date = (new \app\admin\model\Cate())->cateselect();
        $admin = session('admin_user')['username'];
        return view('admin@blog/index', compact( 'admin','dete', 'data', 'date'));
    }

    public function blogadd(Request $request)
    {
        if ($request->isGet()) {
            $data = (new Admin())->insert();
            $date = (new \app\admin\model\Cate())->cateselect();
            $admin = session('admin_user')['username'];
            return view('admin@blog/blogadd', compact('admin', 'data', 'date'));
        } else if ($request->isPost()) {
            $data = $request->post();
            $shuju =(new \app\admin\model\Blog())->blogcreate($data);
            if($shuju==1){
                return 1;
            }else{
                return $shuju;
            }
        }
    }

    public function blogupd(Request $request)
    {
        if($request->isGet()){
            $id =$request->get('id');
            $dede =(new \app\admin\model\Blog())->blogchaxun($id);
            $data =(new Admin())->insert();
            $date = (new \app\admin\model\Cate())->cateselect();
            return view('admin@blog/blogupd', compact('dede','data','date'));
        }else if($request->isPost()){
            $data = $request->post();
            $id =$data['id'];
            $shuju =(new \app\admin\model\Blog())->blogupdate($id,$data);
            if($shuju==1){
                return 1;
            }else{
                return $shuju;
            }
        }

    }

    public function blogdel(Request $request)
    {
        $id =$request->get('id');
        if((new \app\admin\model\Blog())->blogdelete($id)){
            return 1;
        }else{
            return 2;
        }
    }

    public function blogsearch(Request $request)
    {
            $data = (new Admin())->insert();
            $date = $request->post();
            $admin = session('admin_user')['username'];
            $shuju = (new \app\admin\model\Blog())->blogsearch($date);
            return view('admin@blog/blogsearch', compact('shuju', 'admin', 'data'));

    }

    public function update(Request $request)
    {
        //接收图片信息
        $file = $request->file('image');
        //附件存储临时位置
        $path = "./bloguploads/mylogo";
        //系统会给附件创建一个随机的字符串
        $result = $file->move($path);
        if ($result) {
            //上传成功，返回当前上传的附件路径名，用于客户端使用
            $logoname = $result->getPathname();
            $logo = str_replace("\\", "/", $logoname);
            $info = ['status' => 'success', 'logo' => $logo];
            echo json_encode($info);
        } else {
            $info = ['status' => 'failure'];
            echo json_encode($info);
        }
    }
}
